<?php
/**
 * Created by IntelliJ IDEA.
 * 后台管理员
 * User: xmiao
 * Date: 2018/12/11
 * Time: 15:07
 */

namespace app\magic\dao;
use \app\magic\model\Admin;
class AdminDao
{
    private $admin = null;

    /**
     * AdminDao constructor.
     */
    public function __construct()
    {
        //初始化model对象
        $this->admin = new Admin();
    }

    /**
     * 查询单条数据
     */
    public function find($data){
        return $this->admin->where($data)->find()->toArray();
    }

    /**
     * 查询数据
     * @param null $data 查询条件
     * @return array 数据数组
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectDao($data=null){
        if($data){
            $res= $this->admin->where($data)->select();
        }else {
            $res = $this->admin->select();
        }
        if ($res!=null) {
            return $res->toArray();
        }else{
            return null;
        }
    }
    public function numDao($data=null){
        if($data){
            $res= $this->admin->where($data)->count();
        }else {
            $res = $this->admin->count();
        }
        if ($res!=null) {
            return $res;
        }else{
            return null;
        }
    }
    /**
     * 添加操作
     * 有主键唯一字段为修改，否则为添加
     * @param $data 添加数据
     * @return false|int是否执行成功
     */
    public function operationDao($data){
        if($data['id']){
            return $this->admin->isUpdate(true)->save($data);
        }else{
            return $this->admin->isUpdate(false)->save($data);
        }
    }
}